/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.fyx.mula.web;

import java.util.Observable;
import java.util.Observer;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import org.fyx.mula.ejb.MulaLocal;
import org.fyx.mula.ejb.MulaRoles;
import org.fyx.core.web.event.MenuCreatingEventHandler;
import org.fyx.core.web.event.RoleCreatingEventHandler;
import org.fyx.linker.web.LinkerLocal;
import org.fyx.secure.entity.SecureRole;
import org.fyx.secure.ejb.SecureLocal;
import org.primefaces.component.menuitem.MenuItem;

/**
 * Mula observer pro naslouchani na vnitrni udalosti.
 * Zoodpovida za spravne provedeni akci na urcite udalosti.
 * 
 * @author ydenek
 */
@Stateless
@LocalBean
public class MulaObserver implements Observer {

    @EJB
    private SecureLocal secure;
    @EJB
    private LinkerLocal linker;
    @EJB
    private MulaLocal mula;

    public MulaObserver() {
        super();
    }
    
    

    @Override
    public void update(Observable o, Object arg) {
        if (o instanceof MenuCreatingEventHandler) {
            menuCreate();
        } else if (o instanceof RoleCreatingEventHandler) {
            roleCreate();
        }
    }

    private void menuCreate() {
        MenuItem manageLanguage = new MenuItem();
        manageLanguage.setValue(mula.get("mula_managerLanguages"));
        manageLanguage.setIcon("ui-icon ui-icon-flag");
        manageLanguage.setUrl(linker.getLink("mula/list.xhtml"));
        manageLanguage.setId("mula_managerLanguages");
        linker.addMenuItem(manageLanguage, MulaRoles.LANGUAGE_MANAGER);
    }

    private void roleCreate() {
        SecureRole languageManager = new SecureRole(MulaRoles.LANGUAGE_MANAGER);
        languageManager.setCategory("Languages");
        languageManager.setTitle("Languages manager");
        languageManager.setDescription("Creating translation of texts.");
        secure.addRole(languageManager);
    }
}
